package com.mollytask;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.mollytask.bean.MyUser;
import com.mollytask.institution.DataFillFragment;
import com.mollytask.institution.InformationFragment;
import com.mollytask.institution.ReviewResultFragment;
import com.mollytask.login.WelcomeActivity;
import com.mollytask.superadmin.DataCheckFragment;
import com.mollytask.superadmin.DataRecordFragment;
import com.mollytask.superadmin.DataRemindFragment;
import com.mollytask.superadmin.SuperAdminMessageFragment;
import com.mollytask.util.BottomNavigationViewHelper;

import cn.bmob.v3.BmobUser;

import static com.mollytask.R.id.navigation;
import static com.mollytask.R.id.viewPager;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {


    private ViewPager mViewPager;

    private BottomNavigationView mBottomView;

    private ViewPager mSuperViewPager;

    private BottomNavigationView mSuperBottomView;

    private ImageView mLogOut;
    private ImageView mSuLogOut;

    private ImageView mEnergyLogout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
        int userType = myUser.getType();
        if (userType == 0) {
            setContentView(R.layout.institute_layout);
            initInstitute();
        } else if (userType == 1) {
            setContentView(R.layout.super_admin_layout);
            initSuperAdmin();
        } else {
            setContentView(R.layout.energy_manage_layout);
            initEnergyManage();
        }

    }

    private void initInstitute() {

        mViewPager = (ViewPager) findViewById(viewPager);

        mBottomView = (BottomNavigationView) findViewById(navigation);

        mLogOut = (ImageView) findViewById(R.id.log_out);

        mViewPager.addOnPageChangeListener(this);
        mBottomView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return DataFillFragment.newInstance();
                    case 1:
                        return ReviewResultFragment.newInstance();
                    case 2:
                        return InformationFragment.newInstance();
                }
                return null;
            }

            @Override
            public int getCount() {
                return 3;
            }
        });

        mLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quit();
            }
        });

    }

    //初始化 能管局界面
    private void initEnergyManage() {

        mEnergyLogout = (ImageView) findViewById(R.id.energy_log_out);
        mEnergyLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quit();
            }
        });

    }

    //初始化 上级行政部门界面
    private void initSuperAdmin() {

        mSuperViewPager = (ViewPager) findViewById(R.id.viewPager1);

        mSuperBottomView = (BottomNavigationView) findViewById(R.id.navigation1);
        BottomNavigationViewHelper.disableShiftMode(mSuperBottomView);

        mSuLogOut = (ImageView) findViewById(R.id.su_log_out);

        mSuperViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mSuperBottomView.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mSuperBottomView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home1:
                        mSuperViewPager.setCurrentItem(0);
                        break;
                    case R.id.navigation_dashboard1:
                        mSuperViewPager.setCurrentItem(2);
                        break;
                    case R.id.navigation_notifications1:
                        mSuperViewPager.setCurrentItem(3);
                        break;
                    case R.id.navigation_record:
                        mSuperViewPager.setCurrentItem(1);
                        break;
                }
                return true;
            }
        });

        mSuperViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return DataCheckFragment.newInstance();
                    case 1:
                        return DataRecordFragment.newInstance();
                    case 2:
                        return DataRemindFragment.newInstance();
                    case 3:
                        return SuperAdminMessageFragment.newInstance();
                }
                return null;
            }

            @Override
            public int getCount() {
                return 4;
            }
        });

        mSuLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quit();
            }
        });
    }


    public void quit() {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .title("退出确认")
                .content("确认要退出吗？")
                .positiveText("退出")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        BmobUser.logOut();
                        Toast.makeText(MainActivity.this, "退出当前账号", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
                        startActivity(intent);
                        finish();
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                    }
                })
                .show();

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mViewPager.setCurrentItem(0);
                    break;
                case R.id.navigation_dashboard:
                    mViewPager.setCurrentItem(1);
                    break;
                case R.id.navigation_notifications:
                    mViewPager.setCurrentItem(2);
                    break;

            }
            return true;
        }

    };

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mBottomView.getMenu().getItem(position).setChecked(true);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
